import 'package:flutter/material.dart';
import 'package:flutter_comm_base_framework/core/select_model.dart';
import 'package:flutter_comm_base_framework/core/utils/ColorsUtil.dart';

class TabView extends StatefulWidget {

  List<SelectModel> items;
  List<String> itemsName;
  int currentIndex = 0;
  double itemMinWidth = 10;
  bool userInteractionEnabled = true;
  TabViewState _state;


  TabView({List<SelectModel> items,this.itemMinWidth=0,List<String> itemsName,this.currentIndex=0,this.userInteractionEnabled = true})//: assert((items == null && itemsName == null) || items != null && itemsName != null)
  {
    if(items == null && itemsName != null){
      List<SelectModel> items = List<SelectModel>();
      for(int i=0;i<itemsName.length;i++){
        String name = itemsName[i];
        items.add(SelectModel(name,name,isSelect: i==this.currentIndex));
      }
      this.items = items;
    }else{
      this.items = items;
      this.itemsName = itemsName;
    }
  }

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    _state = TabViewState();
    return _state;
  }

  void setCurrentIndex(int index){
    if(currentIndex != index && (index>=0 && index < items.length)){
      this.items[currentIndex].isSelect = false;
      this.items[index].isSelect = true;
      currentIndex = index;
      refreshViews();
    }
  }

  void refreshViews(){
    if(_state != null){
      _state.setState(() { });
    }
  }

}

class TabViewState extends State<TabView>{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
        children: widget.items.map(
                (SelectModel model) =>
                    TabItem("${model.name}",isSelect: model.isSelect,userInteractionEnabled: widget.userInteractionEnabled,constraints: BoxConstraints(minWidth: widget.itemMinWidth),
                  onSelectChanged: (bool selected){
                    selectItem(model);
                  },)
        ).toList());
  }


  void selectItem(SelectModel item){

    List<SelectModel> items = widget.items;
    for(int i=0;i<items.length;i++){
      SelectModel itemi = items[i];
      itemi.isSelect = itemi==item;
    }
    setState(() {

    });
  }
}

class TabItem extends StatefulWidget {
  bool isSelect = false;
  String title;
  Function(bool) onSelectChanged;
  BoxConstraints constraints;
  bool userInteractionEnabled = true;

  TabItem(this.title, {this.isSelect = false,this.onSelectChanged,this.constraints,this.userInteractionEnabled = true});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TabItemState();
  }
}

class TabItemState extends State<TabItem> {
  get children => null;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    Color bgColor;
    TextStyle titleStyle;
    if(widget.isSelect){
      bgColor = Color(0xff477bfc);
      titleStyle = TextStyle(color: Color(0xffffffff));
    }else{
      bgColor = Color(0xffedeeef);
      titleStyle = TextStyle(color: Color(0xff333333));
    }
    return
      GestureDetector(
        child:Container(
          constraints: widget.constraints,
          width: widget.constraints.minWidth,
          //constraints: widget.constraints,
            child: widget.isSelect?
            Column(
                children:<Widget>[
                  Container(height: 30,child:Text(widget.title,style: TextStyle(color: ColorsUtil.blue_dark1,fontSize: 14),),),
                  Container(color: ColorsUtil.btn_background,height: 2)
                ] ):
            Column(
                children:<Widget>[
                  Container(height: 30,child:Text(widget.title,style: TextStyle(color: ColorsUtil.black_999,fontSize: 14),),),
                  Container(margin: EdgeInsets.only(top: 0.5),color: ColorsUtil.black_999,height: 1),
                ] )),onTap: (){
          if(widget.userInteractionEnabled){
            setState(() {
              if(widget.onSelectChanged != null){
                widget.isSelect = !widget.isSelect;
                widget.onSelectChanged(widget.isSelect);
              }
            });
          }
      },);
  }

}
